package com.taotao.manage.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.manage.mapper.ItemMapper;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.pojo.ItemDesc;

@Service
public class ItemService extends BaseService<Item> {

	@Autowired
	ItemMapper itemMapper;

	@Autowired
	ItemDescService itemDescService;
	
	/**
	 * 确保添加item和itemDesc为同一事务
	 * @param item
	 * @param itemDesc
	 * @return
	 */
	public Boolean saveItemAndItemDesc(Item item, String desc) {
		// 设置item状态
		item.setStatus(1);
		// 确保item的id自增
		item.setId(null);
		Boolean flag = super.save(item);
		if (flag) {
			ItemDesc itemDesc = new ItemDesc();
			// 设置itemDesc的itemId属性
			itemDesc.setItemId(item.getId());
			itemDesc.setItemDesc(desc);
			flag = itemDescService.save(itemDesc);
		}
		if (flag) return true;
		return false;
	}

	public Boolean updateItemAndItemDesc(Item item, String desc) {
		Boolean update = super.update(item);
		if (update) {
			ItemDesc itemDesc = new ItemDesc();
			itemDesc.setItemId(item.getId());
			itemDesc.setItemDesc(desc);
			update = itemDescService.update(itemDesc);
		}
		if (update) return true;
		return false;
	}

}
